<template>
	<view class="qrcodePage">
		<NavigationTitle title="扫码详情" style="color: #000;background: #fff;position: fixed;width: 100%;z-index: 5;">
			<template slot="left_con">
				<image class="back_icon" src="@/static/common/back.png" mode="widthFix" @tap="backSelf" />
			</template>
		</NavigationTitle>
		<view class="status-bar"></view>
		<view class="main">
			<uni-forms ref="userForm" :modelValue="data" class="form_main" v-if="type==1">
				<uni-forms-item label="人员名称:" name="nickName">
					<uni-easyinput v-model="data.nickName"  disabled />
				</uni-forms-item>
				<uni-forms-item label="手机号码:" name="userPhone">
					<uni-easyinput v-model="data.userPhone" disabled />
				</uni-forms-item>
				<uni-forms-item label="所属公司:" name="compName">
					<uni-easyinput v-model="data.compName" disabled />
				</uni-forms-item>
				<uni-forms-item label="人员角色:" name="userRole">
					<uni-easyinput v-model="data.userRole" disabled />
				</uni-forms-item>
				<uni-forms-item label="公司所属:" name="pname">
					<uni-easyinput v-model="data.pname" disabled />
				</uni-forms-item>
			</uni-forms>
			<uni-forms ref="deviceForm" :modelValue="data" class="form_main" v-if="type==2">
				<uni-forms-item label="设备编号:" name="deviceId">
					<uni-easyinput v-model="data.deviceId"  disabled />
				</uni-forms-item>
				<uni-forms-item label="设备名称:" name="deviceName">
					<uni-easyinput v-model="data.deviceName"  disabled />
				</uni-forms-item>
				<uni-forms-item label="设备类型:" name="deviceType">
					<uni-easyinput v-model="data.deviceTypeString" disabled />
				</uni-forms-item>
				<uni-forms-item label="设备状态:" name="deviceStatus">
					<uni-easyinput v-model="data.deviceStatusString" disabled />
				</uni-forms-item>
				<uni-forms-item label="GPS编号:" name="gpsNumber">
					<uni-easyinput v-model="data.gpsNumber" disabled />
				</uni-forms-item>
				<uni-forms-item label="创建时间:" name="createTime">
					<uni-easyinput v-model="data.createTime" disabled />
				</uni-forms-item>
				<button type="primary" @click="bindingEquip">绑定设备</button>
			</uni-forms>
		</view>
	</view>
</template>

<script>
	import NavigationTitle from '@/components/navigationTitle'
	import {
		showToastMsg,
	} from '@/utils/format.js'
	export default {
		components: {
			NavigationTitle
		},
		data() {
			return {
				data: {},
				type: 0,
				projectId:'',
			}
		},
		onLoad(option) {
			console.log(option)
			let {
				id,
				projectId,
				userId,
				type
			} = option
			let param = {}
			this.type = type
			this.projectId = projectId
			param.projectId = projectId
			if (option.type == 1) { //用户
				param.userId = userId
				this.getUserDetail(param)
			} else if (option.type == 2) { //设备
				param.id = id
				this.getEquipDetail(param)
			}
			
		},
		methods: {
			backSelf() {
				uni.navigateBack({
					delta: 1
				})
			},
			async getUserDetail(data) {
				await this.$api.getUserDetail(data).then(res => {
					if (res?.code == 200) {
						this.data = res.data
					}
					uni.showToast({
						title: res.msg,
						duration: 1000
					})
				})
			},
			async getEquipDetail(data) {
				await this.$api.getEquipmentDetail(data).then(res => {
					if (res?.code == 200) {
						this.data = res.data
					}
					uni.showToast({
						title: res.msg,
						duration: 1000
					})
				})
			},
			 bindingEquip() {
				if(!this.data.id) return 
				let param = {
					 deviceId: this.data.deviceId,
					  deviceName: this.data.deviceName,
					  deviceStatus: this.data.deviceStatus,
					  deviceType: this.data.deviceType,
					  gpsNumber: this.data.gpsNumber,
					  id: this.data.id,
					  project: this.projectId,
					  users: uni.getStorageSync('userInfo').userId
				},that = this
				uni.showModal({
					title: '提示',
					content: '是否确定绑定这个设备!',
					success: async function (res) {
						if (res.confirm) {
							console.log('用户点击确定');
							const res = await that.$api.editEquipment(param).catch(err => {
								console.log(err)
							})
							if (res?.code == 200) {
								setTimeout(()=>{that.backSelf()},1000)
							}
							showToastMsg(res.msg)
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
				
				
			},
		}
	}
</script>

<style lang="scss" scoped>
	@import '@/assets/styles/global.scss';

	view {
		box-sizing: border-box;
	}

	.qrcodePage {
		
		.main {
			padding: $System__title_height 12rpx 6rpx;
		}
		.form_main {
			padding: 40rpx 20rpx;
			min-height: 550rpx;
			.uni-forms-item {
				flex-direction: column;
			
				/deep/ .uni-forms-item__label {
					width: auto;
					white-space: nowrap;
					font-size: 32rpx;
				}
				/deep/ .is-disabled {
					background-color: #fff !important;
					color: #333;
				}
			}
		}

		.back_icon {
			width: 37upx;
		}
	}
</style>